package com.wangzhixuan.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wangzhixuan.commons.constant.Constants;
import com.wangzhixuan.commons.utils.PageInfo;
import com.wangzhixuan.commons.utils.StringUtils;
import com.wangzhixuan.mapper.BaseInfoMapper;
import com.wangzhixuan.mapper.ImageMapper;
import com.wangzhixuan.mapper.SurveyBaseMapper;
import com.wangzhixuan.mapper.SurveyPeopleMapper;
import com.wangzhixuan.model.BaseInfo;
import com.wangzhixuan.model.Image;
import com.wangzhixuan.model.SurveyBase;
import com.wangzhixuan.model.SurveyPeople;
import com.wangzhixuan.model.vo.SurveyBaseVo;
import com.wangzhixuan.service.ISurveyBaseService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-07-22
 */
@Service
public class SurveyBaseServiceImpl extends ServiceImpl<SurveyBaseMapper, SurveyBase> implements ISurveyBaseService {
	
	@Autowired
    private SurveyBaseMapper surveyBaseMapper;
	
	@Autowired
    private SurveyPeopleMapper surveyPeopleMapper;
    
	@Autowired
    private ImageMapper imageMapper;
	
	@Autowired
    private BaseInfoMapper baseInfoMapper;
	
	
	@Override
    public void selectDataGrid(PageInfo pageInfo) {
    	Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<SurveyBase> list = surveyBaseMapper.selectSurveyBasePage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

	@Override
	public boolean insertSurveyBaseInfo(SurveyBaseVo surveyBaseVo) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		SurveyBase surveyBase = new SurveyBase();
		PropertyUtils.copyProperties(surveyBase, surveyBaseVo);
		surveyBaseMapper.insert(surveyBase);
		SurveyPeople surveyPeople = null;
		for(String userId : surveyBaseVo.getSurveyRtaffArrList()){
			if(StringUtils.isNotBlank(userId)){
				surveyPeople = new SurveyPeople();
				surveyPeople.setWorkId(surveyBaseVo.getWorkId());
				surveyPeople.setSurveyRtaffId(Integer.parseInt(userId));
				surveyPeople.setCreateTime(new Date());
				surveyPeopleMapper.insert(surveyPeople);
			}
		}
		
		Image image = null;
		for(String str : surveyBaseVo.getChargedImgArrList()){
			if(StringUtils.isNotBlank(str)){
				image = new Image();
				image.setWorkId(surveyBaseVo.getWorkId());
				image.setImgPath(str);
				image.setType(Constants.IMAGE_STATUS.STATUS1);
				image.setCreateTime(new Date());
				imageMapper.insert(image);
			}
		}
		
		for(String str : surveyBaseVo.getWorkImgArrList()){
			if(StringUtils.isNotBlank(str)){
				image = new Image();
				image.setWorkId(surveyBaseVo.getWorkId());
				image.setImgPath(str);
				image.setType(Constants.IMAGE_STATUS.STATUS2);
				image.setCreateTime(new Date());
				imageMapper.insert(image);
			}
		}
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("status", Constants.WORK_STATUS.STATUS2);
		paramMap.put("workId", surveyBaseVo.getWorkId());
		baseInfoMapper.updateStatusByWorkId(paramMap);
		BaseInfo baseInfo = baseInfoMapper.selectById(surveyBaseVo.getWorkId());
		baseInfo.setSurveyStatus(303);									//现场勘察
		baseInfoMapper.updateById(baseInfo);
		return true;
	}

	@Override
	public Map<String, Object> selectSurveyDetail(Integer workId) {
		Map<String, Object> retMap = surveyBaseMapper.selectByWorkId(workId);
		if(retMap==null || retMap.size()==0){
			retMap = new HashMap<String,Object>();
			retMap.put("surveyRespId", "");
			retMap.put("surveyRespName", "");
			retMap.put("surveyRespPid", "");
			retMap.put("surveyTime", "");
			retMap.put("surveyName", "");
			retMap.put("surveyContent", "");
			retMap.put("blackoutRange", "");
			retMap.put("chargedTest", "");
			retMap.put("workTest", "");
			retMap.put("security", "");
		}
		List<Map<String,Object>> listPeoples = surveyPeopleMapper.selectPeoplesByWorkid(workId);
		retMap.put("surveyRtaffArr", listPeoples);
		
		Map<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("type", Constants.IMAGE_STATUS.STATUS1);
		paramMap.put("workId", workId);
		List<String> listchargedImgArr = imageMapper.selectImagesByWorkIdAndType(paramMap);
		retMap.put("chargedImgArr", listchargedImgArr);
		paramMap.put("type", Constants.IMAGE_STATUS.STATUS2);
		List<String> listworkImgArr = imageMapper.selectImagesByWorkIdAndType(paramMap);
		retMap.put("workImgArr", listworkImgArr);
		return retMap;
	}

	@Override
	public Integer selectSurveyByWordId(Integer workId) {
		Integer id = surveyBaseMapper.selectSurveyByWordId(workId);
		return id;
	}
}
